package com.ikangtai.shecare.stickycalendar.adapter;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ikangtai.shecare.R;
import com.ikangtai.shecare.common.App;
import com.ikangtai.shecare.common.baseView.pickerDialog.recordPickerDialog.TemperaturePickerDialog;
import com.ikangtai.shecare.common.util.AppLog;
import com.ikangtai.shecare.stickycalendar.RecordMoreActivity;
import com.ikangtai.shecare.stickycalendar.bean.ListBean;
import com.ikangtai.shecare.stickycalendar.fragment.widget.GroupButtonView;
import com.ikangtai.shecare.stickycalendar.http.MensesHttp;
import com.ikangtai.shecare.stickycalendar.http.SexHttp;
import com.ikangtai.shecare.stickycalendar.http.SymptomHttp;
import com.ikangtai.shecare.stickycalendar.http.util.MensesMsg;
import com.ikangtai.shecare.stickycalendar.http.util.SexMsg;
import com.ikangtai.shecare.stickycalendar.http.util.SymptomMsg;
import com.ikangtai.shecare.stickycalendar.init.InitRecordMenses;
import com.kyleduo.switchbutton.SwitchButton;
import com.orhanobut.dialogplus.DialogPlus;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lijinyuan on 16/4/26.
 */
public class ListItemAdapter extends BaseAdapter implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    private ViewHolder viewHolder;
    private Context mContext;
    private List<ListBean> mListBeen;
//    private View menstruationView;
    private PhyListAdapter phyListAdapter;

    private List<MensesMsg> mensesMsgList;
    private List<SexMsg> sexMsgList;
    private List<SymptomMsg> symptomMsgList;


    public ListItemAdapter(Context mContext, List<ListBean> mListBeen, PhyListAdapter adapter) {
        this.mContext = mContext;
        this.mListBeen = mListBeen;
        this.phyListAdapter = adapter;

    }


    @Override
    public int getCount() {
        return mListBeen.size();
    }

    @Override
    public Object getItem(int position) {
        return mListBeen.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(mContext);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout
                    .list_item_content, null);

            viewHolder.re_menses_detail_arrow = (ImageView) convertView.findViewById(R.id.re_menses_detail_arrow);
            viewHolder.re_physical_symptoms_arrow = (ImageView) convertView.findViewById(R.id.re_physical_symptoms_arrow);
            viewHolder.re_menses = (RelativeLayout) convertView.findViewById(R.id.re_menses);
            viewHolder.re_bbt = (RelativeLayout) convertView.findViewById(R.id.re_bbt);
            viewHolder.re_physical_symptoms = (RelativeLayout) convertView.findViewById(R.id.re_physical_symptoms);
            viewHolder.re_menses_detail = (TextView) convertView.findViewById(R.id.re_menses_detail);
            viewHolder.re_bbt_number = (TextView) convertView.findViewById(R.id.re_bbt_number);
            viewHolder.re_physical_symptoms_content = (TextView) convertView.findViewById(R.id.re_physical_symptoms_content);
            viewHolder.re_physical_symptoms_more = (RelativeLayout) convertView.findViewById(R.id.re_physical_symptoms_more);
            viewHolder.re_menses_detail_more = (RelativeLayout) convertView.findViewById(R.id.re_menses_detail_more);
            viewHolder.re_menses_state = (SwitchButton) convertView.findViewById(R.id.re_menses_state);
            viewHolder.re_fuck_state = (SwitchButton) convertView.findViewById(R.id.re_fuck_state);
            viewHolder.toMore = (Button) convertView.findViewById(R.id.to_more_re);

            viewHolder.re_menses_state.setThumbColorRes(R.color.white);
            viewHolder.re_menses_state.setBackColorRes(R.color.gray2);

            viewHolder.re_fuck_state.setThumbColorRes(R.color.white);
            viewHolder.re_fuck_state.setBackColorRes(R.color.gray2);

            viewHolder.item1= (GroupButtonView) convertView.findViewById(R.id.wzzitem1);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

//        menstruationView = inflater.inflate(R.layout.menstruation_described, null);

        viewHolder.re_bbt.setOnClickListener(this);
        viewHolder.re_menses.setOnClickListener(this);
        viewHolder.re_physical_symptoms.setOnClickListener(this);

        viewHolder.re_menses_state.setOnCheckedChangeListener(this);
        viewHolder.re_fuck_state.setOnCheckedChangeListener(this);
        viewHolder.toMore.setOnClickListener(this);


        viewHolder.item1.setOnGroupBtnClickListener(new GroupButtonView.OnGroupBtnClickListener() {
            @Override
            public void groupBtnClick(String code) {
                AppLog.i("wzz----code---sssssssssssss-");
                AppLog.i("wzz----code----"+code);
            }
        });

        for (int i = 0; i < viewHolder.item1.getViewList().size(); i++) {
            if (viewHolder.item1.getViewList().get(i).getText().equals("重度")) {
                viewHolder.item1.getViewList().get(i).setChecked(true);
            }

        }

        return convertView;
    }

    @Override
    public void onClick(final View v) {
        switch (v.getId()) {
            case R.id.re_menses:
                viewHolder.re_menses_detail_more.setVisibility(
                        viewHolder.re_menses_detail_more.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE
                );
                if (viewHolder.re_menses_detail_more.getVisibility() == View.VISIBLE) {
                    ViewHolder.re_menses_detail_arrow.setImageResource(R.drawable.down_arrow);
                } else
                    ViewHolder.re_menses_detail_arrow.setImageResource(R.drawable.right_arrow);
                notifyDataSetChanged();
                break;
            case R.id.re_physical_symptoms:

                DialogPlus dialog1 = DialogPlus.newDialog(mContext)
                        .setAdapter(phyListAdapter)
                        // This will enable the expand feature, (similar to android L share dialog)
                        .create();
                dialog1.show();

                break;
            case R.id.re_bbt:
                if (App.memory_preference_macAddress == null || App.memory_preference_macAddress.length() <= 0) {
                    Toast.makeText(mContext, R.string.toast_for_unbind, Toast.LENGTH_SHORT).show();
                } else {
                    showTemperatureDialog(viewHolder.re_bbt_number.getText().toString(), 1);
                }
                break;

            case R.id.to_more_re:
                mContext.startActivity(new Intent(mContext, RecordMoreActivity.class));
        }

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.re_menses_state:
                if (isChecked) {
                    viewHolder.re_menses_state.setBackColorRes(R.color.orange);
                    viewHolder.re_menses_state.setThumbColorRes(R.color.white);

                    mensesMsgList = new ArrayList<MensesMsg>();
                    MensesMsg mensesMsg = new MensesMsg();
                    mensesMsg.setMensesHintValue(1);
                    mensesMsg.setReferMensesChoiceValue(0);
                    mensesMsg.setMensesBlood(1);
                    mensesMsg.setDateRecord("2016-08-18");
                    mensesMsgList.add(mensesMsg);
                    MensesHttp mensesHttp = new MensesHttp(mContext,mensesMsgList);
                    mensesHttp.saveSelectedDayRecord();
                } else {
                    viewHolder.re_menses_state.setThumbColorRes(R.color.white);
                    viewHolder.re_menses_state.setBackColorRes(R.color.gray2);

                    mensesMsgList = new ArrayList<MensesMsg>();
                    MensesMsg mensesMsg = new MensesMsg();
                    mensesMsg.setMensesHintValue(1);
                    mensesMsg.setReferMensesChoiceValue(1);
                    mensesMsg.setMensesBlood(0);
                    mensesMsg.setDateRecord("2016-08-18");
                    mensesMsgList.add(mensesMsg);
                    MensesHttp mensesHttp = new MensesHttp(mContext,mensesMsgList);
                    mensesHttp.saveSelectedDayRecord();

                }
                Toast.makeText(mContext, isChecked + "re_menses_state", Toast.LENGTH_SHORT).show();
                break;
            case R.id.re_fuck_state:
                if (isChecked) {
                    viewHolder.re_fuck_state.setBackColorRes(R.color.orange);
                    viewHolder.re_fuck_state.setThumbColorRes(R.color.white);

                    sexMsgList = new ArrayList<SexMsg>();
                    SexMsg sexMsg = new SexMsg();
                    sexMsg.setDateRecord("2016-08-29");
                    sexMsg.setHadSex(1);
                    sexMsgList.add(sexMsg);
                    SexHttp SexHttp = new SexHttp(mContext,sexMsgList);
                    SexHttp.saveSelectedDayRecord();

//                    InitRecordMenses initRecordMenses =new InitRecordMenses(mContext,"2016-08-10");
//                    initRecordMenses.setCurrentRecordMensesType();

                } else {
                    viewHolder.re_fuck_state.setThumbColorRes(R.color.white);
                    viewHolder.re_fuck_state.setBackColorRes(R.color.gray2);

                    sexMsgList = new ArrayList<SexMsg>();
                    SexMsg sexMsg = new SexMsg();
                    sexMsg.setDateRecord("2016-08-29");
                    sexMsg.setHadSex(0);
                    sexMsgList.add(sexMsg);
                    SexHttp SexHttp = new SexHttp(mContext,sexMsgList);
                    SexHttp.saveSelectedDayRecord();

//                    InitRecordMenses initRecordMenses =new InitRecordMenses(mContext,"2016-08-10");
//                    initRecordMenses.setCurrentRecordMensesType();

                }

                Toast.makeText(mContext, isChecked + "re_fuck_state", Toast.LENGTH_SHORT).show();
                break;
        }
    }


    static class ViewHolder {

        private Button toMore;
        private static ImageView re_menses_detail_arrow, re_physical_symptoms_arrow;
        private RelativeLayout re_menses, re_bbt, re_physical_symptoms;
        private TextView re_menses_detail, re_bbt_number, re_physical_symptoms_content;
        private RelativeLayout re_physical_symptoms_more, re_menses_detail_more;
        private SwitchButton re_menses_state, re_fuck_state;

        private GroupButtonView item1, item2, item3, item4;
    }


    private void showTemperatureDialog(String currentTemper, final int position) {
        //看不懂江涛的逻辑
        TemperaturePickerDialog dialog;
        dialog = new TemperaturePickerDialog(mContext, "36.50");
        dialog.setOnTemperatureeSetListener(new TemperaturePickerDialog.OnTemperatureSetListener() {
            @Override
            public void OnTemperatureSet(AlertDialog dialog, String temperature) {
                if (position == 1) {
                    viewHolder.re_bbt_number.setText(temperature);
                }
            }
        });
        dialog.show();
        dialog.setCancelable(false);


    }



}
